﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using CivilRegistry.ControlledModel;

namespace CivilRegistry.Common
{
    public class SessionHandler
    {
        private User user;

        public SessionHandler()
        {
            this.SortDirection = SortDirection.Ascending;
        }

        // Gets the current session.
        public static SessionHandler CurrentSession
        {
            get
            {
                SessionHandler session =
                  (SessionHandler)HttpContext.Current.Session["SessionId"];
                if (session == null)
                {
                    session = new SessionHandler();
                    HttpContext.Current.Session["SessionId"] = session;
                }
                return session;
            }
        }

        public User User
        {
            get
            {
                if (Mediator.EntitiesToRefresh.Contains("User"))
                {
                    this.user = ControlledModel.User.GeUser(user.UserName, user.Password);
                }
                return user;
            }
            set { this.user = value; }
        }

        public SortDirection SortDirection { get; set; }
    }
}